from django.db import models
from luffyapi.utils.models import BaseModel
# Create your models here.


class CourseCategory(BaseModel):
    """
    课程分类
    """
    name = models.CharField(max_length=50, unique=True, verbose_name="分类名称")


    class Meta:
        db_table = "ly_course_category"
        verbose_name = "课程分类"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

from ckeditor_uploader.fields import RichTextUploadingField
class Course(BaseModel):
    """
    实战课程
    """
    course_type = (
        (0,'付费'),
        (1,'VIP专享'),
        (2,'学位课程'),
    )
    level_choices = (
        (0,'初级'),
        (1,'中级'),
        (2,'高级')
    )
    status_choices = (
        (0,'上线'),
        (1,'下线'),
        (2,'预上线'),
    )
    name = models.CharField(max_length=128, verbose_name="课程名称")
    course_img = models.ImageField(upload_to="course", max_length=255, verbose_name="封面图片", blank=True, null=True)
    course_type = models.SmallIntegerField(choices=course_type, default=0, verbose_name="付费类型")
    course_video = models.FileField(upload_to="course", max_length=255, verbose_name="封面视频", blank=True, null=True)
    brief = RichTextUploadingField(verbose_name="详情介绍", null=True, blank=True)
    level = models.SmallIntegerField(choices=level_choices, default=1, verbose_name="难度等级")
    pub_date = models.DateField(verbose_name="发布日期", auto_now_add=True)
    period = models.IntegerField(verbose_name="建议学习周期（day）", default=7)
    attachment_path = models.FileField(upload_to="course_attachment",max_length=255, verbose_name="课程路径", null=True, blank=True)
    status = models.SmallIntegerField(choices=status_choices, default=0, verbose_name="课程状态")
    course_category = models.ForeignKey("CourseCategory", on_delete=models.DO_NOTHING, null=True, blank=True, verbose_name="课程分类")
    students = models.IntegerField(verbose_name="学习人数",default=0)
    lessons = models.IntegerField(verbose_name="总课时数量", default=0)
    pub_lessons = models.IntegerField(verbose_name="课时更新数量", default=0)
    price = models.DecimalField(max_digits=6, decimal_places=2, verbose_name="课程原价", default=0)
    teacher = models.ForeignKey("Teacher", on_delete=models.DO_NOTHING, null=True,blank=True, verbose_name="授课老师")

    class Meta:
        db_table = "ly_course"
        verbose_name = "专题课程"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s" % self.name
    @property
    def lesson_list(self):
        """课程列表的推荐课时"""
        data_list = self.course_lesson.filter(is_recomment=True)
        if len(data_list) < 1:
            return []
        print(data_list)
        data = []
        for item in data_list:
            data.append({
                "id": item.id,
                "name": item.name,
                "lesson": item.lesson,
                "section_type":item.section_type,
                "section_link": item.section_link,
                "free_trail": item.free_trail,
            })
        return data

    @property
    def level_text(self):
        return self.level_choices[self.level][1]



class Teacher(BaseModel):
    """
    讲师
    讲师表
    """
    role_choices = (
        (0,'讲师'),
        (1,'导师'),
        (2,'班主任'),
    )
    name = models.CharField(max_length=32, verbose_name="讲师title")
    role = models.SmallIntegerField(choices=role_choices, default=0, verbose_name="讲师身份")
    title = models.CharField(max_length=64, verbose_name="职位，职称")
    signature = models.CharField(max_length=255, verbose_name="导师签名", help_text="导师签名", blank=True, null=True)
    image = models.ImageField(upload_to="teacher", null=True,verbose_name="讲师封面")
    brief = models.TextField(max_length=1024, verbose_name="讲师描述")

    class Meta:
        db_table = "ly_teacher"
        verbose_name = "讲师导师"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s" % self.name



class CourseChapter(BaseModel):
    """课程章节"""
    course = models.ForeignKey("Course", related_name="coursechapters", on_delete=models.CASCADE, verbose_name="课程名称")
    chapter = models.SmallIntegerField(verbose_name="第几章", default=1)
    name = models.CharField(max_length=128, verbose_name="章节标题")
    summary = models.TextField(verbose_name="章节介绍", blank=True, null=True)
    pub_date = models.DateField(verbose_name="发布日期", auto_now_add=True)

    class Meta:
        db_table = "ly_course_chapter"
        verbose_name = "课程章节"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s:(第%s章)%s" % (self.course, self.chapter, self.name)

    @property
    def lesson_list(self):
        """章节下的课时列表"""
        lessons = self.chapter_lesson.filter(is_show=True, is_deleted=False)
        data = []
        for lesson in lessons:
            data.append({
                "id": lesson.id,
                "name": lesson.name,
                "lesson": lesson.lesson,
                "section_type": lesson.section_type,
                "section_link": lesson.section_link,
                "duration": lesson.duration,
                "free_trail": lesson.free_trail,
            })
        return data

class CourseLesson(BaseModel):
    """课程课时"""
    section_type_chioces = (
        (0,'文档'),
        (1,'练习'),
        (2,'视频'),
    )
    chapter = models.ForeignKey("CourseChapter", related_name='chapter_lesson', on_delete=models.CASCADE, verbose_name="课程章节")
    course = models.ForeignKey("Course", related_name="course_lesson", on_delete=models.CASCADE, verbose_name="课程名称")
    name = models.CharField(max_length=128, verbose_name="课时标题")
    lesson = models.IntegerField(default=1, verbose_name="第几课时")
    section_type = models.SmallIntegerField(default=2, choices=section_type_chioces, verbose_name="课时种类")
    section_link = models.CharField(max_length=255,blank=True,null=True, verbose_name="课程链接", help_text="若是vedio，填vid；若是文档填link")
    duration = models.CharField(verbose_name="视频时长", blank=True, null=True, max_length=32) # 仅在前端展示使用
    pub_date = models.DateTimeField(verbose_name="发布时间", auto_now_add=True)
    free_trail = models.BooleanField(verbose_name="是否可试看", default=False)
    is_recomment = models.BooleanField(verbose_name="是否推荐到课程列表", default=False)

    class Meta:
        db_table = "ly_course_lesson"
        verbose_name = "课程课时"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s-%s" % (self.chapter, self.name)













